package com.ifar.audiosdk.utils

import android.app.Activity
import android.app.KeyguardManager
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Rect
import android.os.Build
import android.os.Handler
import android.view.PixelCopy
import android.view.View
import android.view.WindowManager
import androidx.annotation.RequiresApi
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.ToastUtils
import com.ifar.audiosdk.pop.CallInPop
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.XPermission


object CommonUtils {
    private const val MIN_DELAY_TIME = 500 // 两次点击间隔不能少于1000ms
    private var lastClickTime: Long = 0

    /**防多次点击*/
    fun isFastClick(): Boolean {
        var flag = true
        val currentClickTime = System.currentTimeMillis()
        if (currentClickTime - lastClickTime >= MIN_DELAY_TIME) {
            flag = false
        }
        lastClickTime = currentClickTime
        return flag
    }

    /**截屏1*/
    fun screenshot1(activity: Activity) {
        val view: View = activity.window.decorView
        view.isDrawingCacheEnabled = true
        view.buildDrawingCache()
        val bmp = view.drawingCache
        val frame = Rect()
        activity.window.decorView.getWindowVisibleDisplayFrame(frame)
        val statusHeight = frame.top
        val width: Int = ScreenUtils.getScreenWidth()
        val height: Int = ScreenUtils.getScreenHeight()
        var bitmap: Bitmap = Bitmap.createBitmap(bmp, 0, statusHeight, width, height - statusHeight)
        view.destroyDrawingCache()
    }

    /**截屏2*/
    @RequiresApi(Build.VERSION_CODES.O)
    fun screenshot2(activity: Activity) {
        val bgBitmap = Bitmap.createBitmap(
            ScreenUtils.getScreenWidth(),
            ScreenUtils.getScreenHeight(),
            Bitmap.Config.ARGB_8888
        )

        PixelCopy.request(
            activity.window, bgBitmap,
            (PixelCopy.OnPixelCopyFinishedListener { copyResult ->
                if (PixelCopy.SUCCESS == copyResult) {
//                    ivScreen.setImageBitmap(bitmap)
//                    ivScreen.visibility = View.VISIBLE
                } else {
                    // onErrorCallback()
                }
            }), Handler()
        )
    }

    /**锁屏上层显示界面*/
    fun lockScreen(activity: Activity) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O_MR1) {
            activity.setShowWhenLocked(true)
            activity.setTurnScreenOn(true)
            val keyguardManager =
                activity.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
            keyguardManager.requestDismissKeyguard(activity, null)
        } else {
            activity.window.addFlags(
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            )
        }
    }

    /**锁屏上层界面按钮操作*/
    fun lockScreenBt(activity: Activity) {
        val keyguardManager = activity.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
        if (keyguardManager.isDeviceLocked) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                keyguardManager.requestDismissKeyguard(activity,
                    object : KeyguardManager.KeyguardDismissCallback() {
                        override fun onDismissSucceeded() {
                            super.onDismissSucceeded()
                            //屏幕被锁住时，解锁成功后操作
                        }
                    }
                )
            }
        } else {
            //屏幕未被锁住时操作
        }
    }

    /**后台显示对话框*/
    fun bgDialog(activity: Activity) {
        XPopup.requestOverlayPermission(
            activity,
            object : XPermission.SimpleCallback {
                override fun onGranted() {
                    XPopup.Builder(activity)
                        .enableShowWhenAppBackground(true) //允许在应用后台弹出\
                        .isDestroyOnDismiss(true)
                        .asCustom(CallInPop(activity))
                        .show()
                }

                override fun onDenied() {
                    ToastUtils.showShort("权限拒绝需要申请悬浮窗权限！")
                }
            })
    }

    /**通知*/
    fun notification() {
//  startService(Intent(mContext, NotificationGroupService::class.java))
//  startService(Intent(mContext, NotificationService::class.java))
//  startService(Intent(mContext, NotificationMyStyleService::class.java))
    }
}